package com.sinodata.bsm.center.service.resource;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sinodata.bsm.center.cache.AttributeCache;
import com.sinodata.bsm.center.cache.ResAttrValCache;
import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.message.ProbeMessenger;
import com.sinodata.bsm.common.exception.AppException;
import com.sinodata.bsm.common.vo.Attribute;
import com.sinodata.bsm.common.vo.ResAttrVal;
import com.sinodata.bsm.common.vo.ResAttrValHis;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-7 下午4:06:29          liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class AttributeService {
    @Autowired
    private AttributeCache attributeCache;
    @Autowired
    private ResAttrValCache resAttrValCache;
    @Autowired
    private BaseDao baseDao;

    @Transactional(propagation = Propagation.REQUIRED)
    public void saveResAttrVal(ResAttrVal resAttrVal) {
        Date d = new Date();
        resAttrVal.setTime(d);
        baseDao.saveOrUpdate(resAttrVal);
        ResAttrValHis hisVal = new ResAttrValHis();
        hisVal.setAttrId(resAttrVal.getAttrId());
        hisVal.setAttrValue(resAttrVal.getAttrValue());
        hisVal.setTime(d);
        hisVal.setResId(resAttrVal.getResId());
        baseDao.save(hisVal);
        resAttrValCache.put(resAttrVal);
    }

    public ResAttrVal getResAttrVal(Long resId, Long attrId) {
        return resAttrValCache.get(resId, attrId);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteResAttrVal(ResAttrVal resAttrVal) {
        baseDao.delete(resAttrVal);
        resAttrValCache.remove(resAttrVal.getResId(), resAttrVal.getAttrId());
    }

    public List<ResAttrVal> getResAttrValByResId(Long resId) {
        return resAttrValCache.getResAttrValByResId(resId);
    }

    /**
     * 保存自定义属性定义
     * @param attr
     * @throws AppException
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveOrUpdateAttribute(Attribute attr) throws AppException {

        if (attr.getResId() == null) {
            throw new AppException("属性定自义的resId值为空");
        }
        if (attr.getResType() != null) {
            throw new AppException("属性自定义的resType值不为 NULL");
        }
        baseDao.saveOrUpdate(attr);
        attributeCache.put(attr);
        ProbeMessenger.notifyAttributeAdd(attr);
    }

    /**
     * 删除自定义属性定义
     * @param attrId
     * @throws AppException
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteAttributeById(Long attrId) throws AppException {
        Attribute attr = attributeCache.get(attrId.longValue());
        if (attr == null) {
            return;
        }
        if (attr.getResId() == null) {
            throw new AppException("属性定自义的resId值为空");
        }
        if (attr.getResType() != null) {
            throw new AppException("属性自定义的resType值不为 NULL");
        }
        attributeCache.remove(attrId.longValue());
        baseDao.delete(attr);
    }

    /**
     * 获取资源自定义属性
     * @param resId
     * @return
     */
    public List<Attribute> findAttributeByResId(Long resId) {
        return attributeCache.getByResId(resId);
    }

    /**
     * 获取属性
     * @param id
     * @return
     */
    public Attribute getAttributeById(Long id) {
        return attributeCache.get(id);
    }

    /***
     * 获取资源默认属性
     * @param resTypeId
     * @return
     */
    public List<Attribute> findAttributeByResTypeId(Long resTypeId) {
        return attributeCache.getByResTypeId(resTypeId);
    }

}
